Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Text.RegularExpressions

Namespace C4F.DevKit.WebServices
	''' <summary>
	''' Provides useful methods like conversion methods.
	''' </summary>
	Public Class Utility

		Private Sub New()
		End Sub
        ''' <summary>
        ''' The Method converts the temperature in Fahrenheit to Celcius
        ''' </summary>
        ''' <param name="temperature">Temperature in Fahrenheit</param>
        ''' <returns>Temperature in Celcius</returns>
        Public Shared Function FahrenheitToCelcius(ByVal temperature As Double) As Double
            Return (5.0 / 9.0) * (temperature - 32)
        End Function

        ''' <summary>
        ''' The Method converts the temperature in Celcius to Fahrenheit
        ''' </summary>
        ''' <param name="temperature">Temperature in Celcius</param>
        ''' <returns>Temperature in Fahrenheit</returns>
        Public Shared Function CelciusToFahrenheit(ByVal temperature As Double) As Double
            Return (9.0 / 5.0) * temperature + 32
        End Function

        ''' <summary>
        ''' Validates the zip code
        ''' </summary>
        ''' <param name="zipCode">Zip code</param>
        ''' <returns>True if zip code is valid</returns>
        Public Shared Function ValidateZipCode(ByVal zipCode As String) As Boolean
            If String.IsNullOrEmpty(zipCode) Then
                Return False
            End If

            Return Regex.IsMatch(zipCode, "(^[0-9]{5}$)|(^[0-9]{5}-[0-9]{4}$)")
        End Function

        ''' <summary>
        ''' Validates the phone number 
        ''' [Example formats: '212-123-4567', '(212)123-4567', '2121234567', '(212) 123-4567', '(212)-123-4567']
        ''' </summary>
        ''' <param name="phoneNumber">Phone number</param>
        ''' <returns>True if phone number is valid</returns>
        Public Shared Function ValidatePhoneNumber(ByVal phoneNumber As String) As Boolean
            If String.IsNullOrEmpty(phoneNumber) Then
                Return False
            End If

            Return Regex.IsMatch(phoneNumber, "^[(]?[2-9][0-9]{2}[)]?[ -]?[0-9]{3}[ -]?[0-9]{4}$")
        End Function

        ''' <summary>
        ''' Validates the area code
        ''' </summary>
        ''' <param name="areaCode">Area code to be validated. Area code should not start with a 0 or a 1.</param>
        ''' <returns>True, if area code is valid</returns>
        Public Shared Function ValidateAreaCode(ByVal areaCode As String) As Boolean
            If String.IsNullOrEmpty(areaCode) Then
                Return False
            End If

            Return Regex.IsMatch(areaCode, "^[^01][0-9]{2}$")
        End Function

        ''' <summary>
        ''' Validates the exchange code
        ''' </summary>
        ''' <param name="exchangeCode">Exchange code to be validated</param>
        ''' <returns>True, if exchange code is valid</returns>
        Public Shared Function ValidateExchangeCode(ByVal exchangeCode As String) As Boolean
            If String.IsNullOrEmpty(exchangeCode) Then
                Return False
            End If

            Return Regex.IsMatch(exchangeCode, "^[0-9]{3}$")
        End Function

        ''' <summary>
        ''' Validates the local 4 digit phone number
        ''' </summary>
        ''' <param name="phone">Phone number to be validated</param>
        ''' <returns>True, if phone number is valid</returns>
        Public Shared Function ValidateLocalPhoneNumber(ByVal phone As String) As Boolean
            If String.IsNullOrEmpty(phone) Then
                Return False
            End If

            Return Regex.IsMatch(phone, "^[0-9]{4}$")
        End Function
    End Class
End Namespace
